package com.hmsm.sys.po;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hmsm.sys.bean.LocalDept;
import com.hmsm.sys.bean.LocalUser;

/**
 * DeptInfo entity. @author MyEclipse Persistence Tools
 */

public class DeptInfo extends LocalDept<DeptInfo, UserInfo> implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1102768214194313393L;

	private static final Log log = LogFactory.getLog(DeptInfo.class);

	// Fields

	private Integer pkId;

	// Property accessors

	public Integer getPkId() {
		return this.pkId;
	}

	public void setPkId(Integer pkId) {
		this.pkId = pkId;
	}

	private Integer idType;

	public Integer getIdType() {
		return this.idType;
	}

	public void setIdType(Integer idType) {
		this.idType = idType;
	}

	private String name;

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	private String deptNo;

	public String getDeptNo() {
		return this.deptNo;
	}

	public void setDeptNo(String deptNo) {
		this.deptNo = deptNo;
	}

	private Integer parId;

	public Integer getParId() {
		return this.parId;
	}

	public void setParId(Integer parId) {
		this.parId = parId;
	}

	private Integer orgId;

	public Integer getOrgId() {
		return this.orgId;
	}

	public void setOrgId(Integer orgId) {
		this.orgId = orgId;
	}

	private Integer state = 1;

	public Integer getState() {
		return this.state;
	}

	public void setState(Integer state) {
		this.state = state;
	}

	private Integer type;

	public Integer getType() {
		return this.type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	private Integer stateParam;// 状态参数

	private DeptInfo parent;

	public DeptInfo getParent() {
		return (DeptInfo) parent;
	}

	public void setParent(DeptInfo parent) {
		this.parent = parent;
		if(parent!=null&&parent.getLevel()!=null){
			this.level = parent.getLevel()+1;
		}
	}

	private Set<UserOrg> userOrgs = new HashSet<UserOrg>();

	public Set<UserOrg> getUserOrgs() {
		return userOrgs;
	}

	public void setUserOrgs(Set<UserOrg> userOrgs) {
		this.userOrgs = userOrgs;
	}

	private OrgInfo org;

	public OrgInfo getOrg() {
		return org;
	}

	public void setOrg(OrgInfo org) {
		this.org = org;
	}

	private Integer level;

	public Integer getLevel() {
		return level;
	}

	public void setLevel(Integer level) {
		this.level = level;
	}

	private List<DeptInfo> subDepts = new ArrayList<DeptInfo>();

	public List<DeptInfo> getSubDepts() {
		return subDepts;
	}

	public List<DeptInfo> getSubDepts(boolean includeDel) {
		List<DeptInfo> list = null;
		if (includeDel) {
			list = subDepts;
		} else {
			list = new ArrayList<DeptInfo>();
			for (DeptInfo dept : subDepts) {
				if (((DeptInfo)dept).getState() == 1) {
					list.add(dept);
				}
			}
		}
		return list;
	}

	public void setSubDepts(List<DeptInfo> subDepts) {
		this.subDepts = subDepts;
		if (subDepts != null) {
			for (DeptInfo subDept : subDepts) {
				subDept.setParent(this);
			}
		}
	}

	private List<UserInfo> users = new ArrayList<UserInfo>();

	public List<UserInfo> getUsers() {
		return users;
	}

	public void setUsers(List<UserInfo> users) {
		this.users = users;
	}

	/** default constructor */
	public DeptInfo() {
	}

	public DeptInfo(Integer id) {
		this.pkId = id;
	}

	public DeptInfo(String name) {
		this.name = name;
	}

	public DeptInfo(Integer id, String name) {
		this.pkId = id;
		this.name = name;
	}

	public DeptInfo(Integer id, String name, int level) {
		this.pkId = id;
		this.name = name;
		this.level = level;
	}

	/** minimal constructor */
	public DeptInfo(Integer idType, String name, Integer parId, Integer orgId, Integer state, Integer type) {
		this.idType = idType;
		this.name = name;
		this.parId = parId;
		this.orgId = orgId;
		this.state = state;
		this.type = type;
	}

	/** full constructor */
	public DeptInfo(Integer idType, String name, String deptNo, Integer parId, Integer orgId, Integer state, Integer type) {
		this.idType = idType;
		this.name = name;
		this.deptNo = deptNo;
		this.parId = parId;
		this.orgId = orgId;
		this.state = state;
		this.type = type;
	}

	// Property accessors

	public Integer getStateParam() {
		return stateParam;
	}

	public void setStateParam(Integer stateParam) {
		this.stateParam = stateParam;
	}

	public String getAliasName() {
		String aliasName = name;

		if (name.contains("(") && name.contains(")")) {
			String[] aliasStrings = name.split("\\(\\)");
			if (aliasStrings.length > 2) {
				aliasName = aliasStrings[1];
			}
		}

		return aliasName;
	}

	@Override
	public List<DeptInfo> getAllDepts() {
		List<DeptInfo> allDepts = new ArrayList<DeptInfo>();
		if (subDepts != null) {
			for (DeptInfo subDept : subDepts) {
				if (subDept != null) {
					allDepts.add(subDept);
					for (DeptInfo sub1Dept : subDept.getAllDepts()) {
						allDepts.add(sub1Dept);
					}
				} else {
					log.debug("null 谁放进去的！");
				}
			}
		}

		return allDepts;
	}
	
	public List<UserInfo> getAllUsers() {
		List<UserInfo> allUsers = new ArrayList<UserInfo>();
		
		Set<Integer> userIds = new HashSet<Integer>();
		// 添加直属用户
		for(LocalUser user:users){
			if(!userIds.contains(user.getPkId())){
				userIds.add(user.getPkId());
				allUsers.add((UserInfo) user);
			}
		}
		
		// 添加下级部门用户
		for(DeptInfo depart:getAllDepts()){
			for(UserInfo user:depart.getAllUsers()){
				if(!userIds.contains(user.getPkId())){
					userIds.add(user.getPkId());
					allUsers.add(user);
				}
			}
		}
		return allUsers;
	}

	/**
	 * 是否是上级部门下的最后一个部门
	 * @return
	 */
	public boolean isLast() {
		boolean isLast = false;
		
		if (getParent()!=null){
			List<DeptInfo> list = getParent().getSubDepts();
			if (list.size()>0){
				DeptInfo last = list.get(list.size()-1);
				isLast = pkId.equals(last.getPkId());
			}else{
				isLast = true;
				log.debug("怎么回事？");
			}
		}
		
		return isLast;
	}
	
	/**
	 * 检查并不更新部门级别
	 * @return
	 */
	public boolean updateLevel() {
	    boolean update = false;
	    
	    if (level==null){
		level = getRealLevel();
		update = true;
	    }else{
		int realLevel = getRealLevel();
		if (!level.equals(realLevel)){
		    update = true; 
		    level = realLevel;
		}
	    }
	    
	    return update;
	}

	/**
	 * 获取真实级别
	 * @return
	 */
	public int getRealLevel() {
	    int realLevel = 1;
	    
	    if (parent!=null){
		realLevel = parent.getRealLevel()+1;
	    }
	    
	    return realLevel;
	}
	
	public String toString(){
		return this.name;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj!=null){
			if (obj instanceof DeptInfo){
				if (((DeptInfo)obj).getPkId()!=null&&((DeptInfo)obj).getPkId().equals(getPkId())){
					return true;
				}else {
					return false;
				}
			}else {
				return false;
			}
		} else {
			return false;
		}
	}

	/**
	 * 获取所属组织机构根部门(需事先加载所有上级部门)
	 * @return
	 */
	public DeptInfo getOrgRootDept() {
		DeptInfo orgRootDept = null;
		if(isOrganization()){
			orgRootDept = this;
		}else{
			if(parent!=null){
				orgRootDept = parent.getOrgRootDept();
			}
		}
		return orgRootDept;
	}


}